﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyGeneration.dOOdads;
using System.Data;
using POSSystem.BLL;
using System.Globalization;

namespace WebUI.Controls
{
    public partial class OrderItemsCtrl : BaseListControl
    {
        public OrderItems OrderLineItems
        {
            get { return (OrderItems)SourceObject; }
            set { SourceObject = value;  }
        }

        public int DumpID
        {
            get
            {
                if (ViewState[ClientID + "_DumpID"] == null || !(ViewState[ClientID + "_DumpID"] is int))
                    ViewState[ClientID + "_DumpID"] = 0;
                return (int)ViewState[ClientID + "_DumpID"];
            }
            set { ViewState[ClientID + "_DumpID"] = value; }
        }

        public void DeleteCheckedItems()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
            }

            if (checkedItemID != "")
                checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);

            if (checkedItemID != "")
            {
                POSSystem.BLL.OrderItems orderItem = POSSystem.BLL.OrderItems.GetOrderItemByIds(checkedItemID);
                for (int i = 0; i < orderItem.RowCount; i++)
                {
                    orderItem.Deleted = true;
                    orderItem.MoveNext();
                }
                orderItem.Save();
            }
        }

        public Decimal TotalAmt
        {
            get
            {
                if (ViewState[ClientID + "_TotalAmt"] == null || !(ViewState[ClientID + "_TotalAmt"] is Decimal))
                    ViewState[ClientID + "_TotalAmt"] = (Decimal)0;
                return Decimal.Parse(ViewState[ClientID + "_TotalAmt"].ToString(), CultureInfo.InvariantCulture);
            }
            set { ViewState[ClientID + "_TotalAmt"] = value; }
        }

        public Decimal TotalGST
        {
            get
            {
                if (ViewState[ClientID + "_TotalGST"] == null || !(ViewState[ClientID + "_TotalGST"] is Decimal))
                    ViewState[ClientID + "_TotalGST"] = 0;
                return Decimal.Parse(ViewState[ClientID + "_TotalGST"].ToString(), CultureInfo.InvariantCulture);
            }
            set { ViewState[ClientID + "_TotalGST"] = value; }
        }

        public void UpdateTotalAmt()
        {
            TotalGST = 0;
            TotalAmt = 0;

            if (SourceObject != null && SourceObject.RowCount > 0 && !SourceObject.IsColumnNull(OrderItems.ColumnNames.GST) && !SourceObject.IsColumnNull(OrderItems.ColumnNames.TotalAmount))
            {
                SourceObject.Rewind();
                for (int i = 0; i < SourceObject.RowCount; i++)
                {
                    TotalAmt += (decimal)SourceObject.GetColumn(OrderItems.ColumnNames.TotalAmount);
                    TotalGST += (decimal)SourceObject.GetColumn(OrderItems.ColumnNames.GST);
                    SourceObject.MoveNext();
                }
            }
            hidTotalAmt.Value = TotalAmt.ToString();
        }

        public override void Refresh()
        {
            UpdateTotalAmt();
            base.Refresh();
            
        }

        public bool ReadOnly
        {
            get { return ViewState[ClientID + "ReadOnly"] != null ? (bool)ViewState[ClientID + "ReadOnly"] : false; }
            set { ViewState[ClientID + "ReadOnly"] = value; if (value) grid.Columns[7].Visible = false; }
        }

        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            if (hidID.Value == "")
                return;

            string oldFilter = OrderLineItems.Filter;

            try
            {
                OrderLineItems.Filter = OrderItems.ColumnNames.ProductID + "=" + hidID.Value;

                if (OrderLineItems.RowCount > 0)
                    OrderLineItems.MarkAsDeleted();

                OrderLineItems.Filter = oldFilter;

                hidID.Value = "";

                Refresh();
            }
            catch (Exception ex)
            {
                OrderLineItems.Filter = oldFilter;
                ErrorLog.WriteToFile("OrderItemsCtrl.ascx.cs", "btnDelete_OnClick", ex.StackTrace);
            }
        }

        protected void btnChangeQty_OnClick(object sender, EventArgs e)
        {
            if (hidID.Value == "" || hidQty.Value == "")
                return;

            string oldFilter = OrderLineItems.Filter;

            try
            {
                OrderLineItems.Filter = OrderItems.ColumnNames.ProductID + "=" + hidID.Value;

                if (OrderLineItems.RowCount > 0)
                {
                    OrderLineItems.s_Quantity = hidQty.Value;
                    OrderLineItems.TotalAmount = OrderLineItems.Quantity * OrderLineItems.CustomerPrice;
                    if(OrderLineItems.GST > 0)
                        OrderLineItems.GST = Math.Round(OrderLineItems.TotalAmount * SysVars.GST / (SysVars.GST + 100), 2);
                }

                OrderLineItems.Filter = oldFilter;

                hidID.Value = "";
                hidQty.Value = "";

                Refresh();
            }
            catch (Exception ex)
            {
                OrderLineItems.Filter = oldFilter;
                ErrorLog.WriteToFile("OrderItemsCtrl.ascx.cs", "btnChangeQty_OnClick", ex.StackTrace);
            }
        }
    }
}